﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Interop;
using WWLauncher.Libs.WindowsInput.Native;

namespace WWLauncher.Util
{
    public class WindowUtil
    {

        public static void SetOwner(Window window, Window parentWindow)
        {
            SetOwner(window, new WindowInteropHelper(parentWindow).Handle);
        }

        public static void SetOwner(Window window, IntPtr parentHandle)
        {
            WindowInteropHelper helper = new WindowInteropHelper(window);
            helper.Owner = parentHandle;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="window"></param>
        /// <returns></returns>
        public static bool WindowIsTop(Window window)
        {
            IntPtr handle = new WindowInteropHelper(window).Handle;
            IntPtr deskHandle = GetDesktopHandle(window, DesktopLayer.Progman);
            IntPtr topHandle =WinApi. GetForegroundWindow();
            //暂时不能正确获取桌面handle  但发现焦点在桌面时 window title为空
            string windowTitle = GetWindowTitle(topHandle);
            return topHandle.Equals(handle) || topHandle.Equals(deskHandle) || string.IsNullOrEmpty(windowTitle);
        }

        private static string GetWindowTitle(IntPtr handle)
        {
            const int nChars = 256;
            StringBuilder buff = new StringBuilder(nChars);
            if (WinApi.GetWindowText(handle, buff, nChars) > 0)
            {
                return buff.ToString();
            }
            return null;
        }


        private const int GWL_STYLE = -16;
        private const int WS_MAXIMIZEBOX = 0x10000;
        /// <summary>
        /// 禁用窗口最大化
        /// </summary>
        /// <param name="window"></param>
        public static void DisableMaxWindow(Window window)
        {
            try
            {
                var hwnd = new WindowInteropHelper(window).Handle;
                var value = WinApi.GetWindowLong(hwnd, GWL_STYLE);
                WinApi.SetWindowLong(hwnd, GWL_STYLE, (int)(value & ~WS_MAXIMIZEBOX));
            }
            catch { }
        }

        public const int GW_CHILD = 5;

        public static IntPtr GetDesktopHandle(Window window, DesktopLayer layer)
        {
            HandleRef hWnd;
            IntPtr hDesktop = new IntPtr();
            switch (layer)
            {
                case DesktopLayer.Progman:
                    hDesktop = WinApi.FindWindow("Progman", null);//第一层桌面  
                    break;
                case DesktopLayer.Shelldll:
                    hDesktop = WinApi.FindWindow("Progman", null);//第一层桌面    
                    hWnd = new HandleRef(window, hDesktop);
                    hDesktop = WinApi.GetWindow(hWnd, GW_CHILD);//第2层桌面     
                    break;
                case DesktopLayer.FolderView:
                    hDesktop = WinApi.FindWindow("Progman", null);//第一层桌面    
                    hWnd = new HandleRef(window, hDesktop);
                    hDesktop = WinApi.GetWindow(hWnd, GW_CHILD);//第2层桌面      
                    hWnd = new HandleRef(window, hDesktop);
                    hDesktop = WinApi.GetWindow(hWnd, GW_CHILD);//第3层桌面    
                    hWnd = new HandleRef(window, hDesktop);
                    hDesktop = WinApi.GetWindow(hWnd, GW_CHILD);//第4层桌面    
                    break;
            }
            return hDesktop;
        }
        /// <summary>
        /// 设置窗体为TopMost
        /// </summary>
        /// <param name="hWnd"></param>
        public static void SetTopomost(IntPtr hWnd)
        {
            //WindowRect rect = new WindowRect();
            //WinApi.GetWindowRect(hWnd, out rect);
            //WinApi.SetWindowPos(hWnd, WinApi.HWND_TOPMOST, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, 0);
            WinApi.SetWindowPos(hWnd, WinApi.HWND_TOPMOST, 0, 0, 0, 0, WinApi.SWP_NOSIZE | WinApi.SWP_NOMOVE);
        }
    }

    public enum DesktopLayer
    {
        Progman = 0,
        Shelldll = 1,
        FolderView = 2
    }
}